package com.xy.bizportdemo.conversation;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xy.bizportdemo.R;
import com.xy.bizportdemo.database.entity.ConversationItem;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 企业信息列表的itemHolder
 * Created by XIAOYUAN on 2016/11/24.
 */
public class ConversationListItem {
    public ImageView mIvLogo;
    public TextView mTvName;
    public TextView mTvLastTime;
    public TextView mTvLastMessage;

    public Map<String, Object> mExtendMap;

    public String mPhoneNumber = "";
    private boolean mIsScrolling = false;

    public ConversationListItem(View view) {
        // TODO Auto-generated constructor stub
        initItem(view);
    }

    protected void initItem(View view) {
        mTvName = (TextView) view.findViewById(R.id.conversation_item_name_textview);
        mTvLastTime = (TextView) view.findViewById(R.id.conversation_item_lasttime_textview);
        mTvLastMessage = (TextView) view.findViewById(R.id.conversation_item_lastsms_textview);
        mIvLogo = (ImageView) view.findViewById(R.id.conversation_item_logo_imageview);
    }

    public void bind(Context context, ConversationItem item) {
        mPhoneNumber = item.number;
        mTvName.setText(mPhoneNumber);
        mTvLastMessage.setText(item.lastMsg);
        mIvLogo.setImageResource(R.drawable.logo_def);

        mExtendMap = item.getExtendMap();

        long timeLong = item.lastDate;
        Date date = new Date(timeLong);
        SimpleDateFormat sf = new SimpleDateFormat("MM-dd HH:mm");
        mTvLastTime.setText(sf.format(date));

        if (!TextUtils.isEmpty(mPhoneNumber)) {
            bindPublicInfo(context);
        }
    }

    /**
     * 绑定企业Logo,name
     */
    private void bindPublicInfo(Context context) {
//        PublicInfoManager.getInstance(context).loadPublicInfo(mPhoneNumber, new PublicInfoManager.OnPublicInfoListener() {
//            @Override
//            public void onPublicInfo(String phone, String name, BitmapDrawable drawable) {
//                if (phone.equals(mPhoneNumber)) {
//                    if (!TextUtils.isEmpty(name) && mTvName != null) {
//                        mTvName.setText(name);
//                    }
//
//                    if (drawable != null) {
//                        mIvLogo.setImageDrawable(drawable);
//                    }
//                }
//            }
//        }, !mIsScrolling);
    }

    public void setmIsScrolling(boolean isScrolling) {
        mIsScrolling = isScrolling;
    }
}
